package com.zxt.content.copywriting.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxt.content.copywriting.mapper.PublicCopywritingMapper;
import com.zxt.content.copywriting.model.PublicCopywriting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 通用逻辑层-公共文案管理类
 *
 * @author zxt
 */
@Slf4j
@Service
public class PublicCopywritingManager extends ServiceImpl<PublicCopywritingMapper, PublicCopywriting> {

    public List<PublicCopywriting> queryList() {
        return list();
    }


    /**
     * 根据标题获取公共文案列表
     *
     * @param themes 必传
     * @param userId 非必传 排序的用户id
     * @return
     */
    public List<PublicCopywriting> listByThemes(List<String> themes, Long userId) {
        LambdaQueryWrapper<PublicCopywriting> wrapper = PublicCopywriting.gw();
        wrapper.in(PublicCopywriting::getTheme, themes);
        if (Objects.nonNull(userId)) {
            wrapper.ne(PublicCopywriting::getUserId, userId);
        }
        return list(wrapper);
    }

    /**
     * 根据标题和上传的用户获取公共文案列表
     *
     * @param themes
     * @param userId
     * @return
     */
    public List<PublicCopywriting> listByThemesAndUserId(List<String> themes, Long userId) {
        LambdaQueryWrapper<PublicCopywriting> wrapper = PublicCopywriting.gw();
        wrapper.in(PublicCopywriting::getTheme, themes);
        wrapper.eq(PublicCopywriting::getUserId, userId);
        return list(wrapper);
    }

    public void saveBatchAndReturnId(List<PublicCopywriting> saveList) {
        baseMapper.saveBatchAndReturnId(saveList);
    }

    /**
     * 随机获取一条公共文案
     */
    public PublicCopywriting randGetOneByUserId() {
        return baseMapper.randGetOneByUserId();
    }
}
